package com.wyzz.hr.action;

import java.util.List;
import org.apache.commons.lang.StringUtils;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.bo.RoleBo;
import com.wyzz.hr.bo.UserBo;
import com.wyzz.hr.pojo.Role;
import com.wyzz.hr.pojo.Userinfo;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.StringUtil;

public class PasswordUpdateAction extends BaseAction {

	public PasswordUpdateAction() {
	}

	public String execute() throws Exception {
		String no = getCurrentEmpNo();
		if (StringUtils.isEmpty(no))
			return "login";
		UserBo userService = (UserBo) SpringBeanFactory.getBean("userService");
		Userinfo user0 = userService.getUserById(no);
		Userinfo userTemp = userService.getUserById(no);
		List userRoleList = userTemp.getUiRoleList();
		int len = userRoleList.size();
		RoleBo roleService = (RoleBo) SpringBeanFactory.getBean("roleService");
		List roleList = roleService.getRoleList();
		int roleListLen = roleList.size();
		String userRoleInfo = "";
		label0: for (int j = 0; j < len; j++) {
			int i = 0;
			do {
				if (i >= roleListLen)
					continue label0;
				Role tempRole = (Role) roleList.get(i);
				if (tempRole.getRoleNo() == Integer
						.parseInt((String) (String) userRoleList.get(j))) {
					if (j == 0) {
						userRoleInfo = (new StringBuilder()).append(
								userRoleInfo).append(tempRole.getRoleName())
								.toString();
						continue label0;
					}
					if (i % 5 == 0)
						userRoleInfo = (new StringBuilder()).append(
								userRoleInfo).append("<br>").append(
								tempRole.getRoleName()).toString();
					else
						userRoleInfo = (new StringBuilder()).append(
								userRoleInfo).append(" | ").append(
								tempRole.getRoleName()).toString();
					continue label0;
				}
				i++;
			} while (true);
		}

		setRoleInfo(userRoleInfo);
		if (!user0.getUiPassword().equals(user.getUiPassword())) {
			addFieldError("user.uiPassword",
					"\u539F\u5BC6\u7801\u4E0D\u6B63\u786E\uFF01");
			return "input";
		}
		user.setId(user0.getId());
		if (!newpassword.equals(confirmpassword)) {
			addFieldError("confirmpassword",
					"\u91CD\u590D\u5BC6\u7801\u4E0D\u4E00\u81F4\uFF01");
			return "input";
		}
		if (userService.updatePassword(user.getId(), newpassword)) {
			addSuccessInfo("\u5BC6\u7801\u4FEE\u6539\u6210\u529F\u3002");
			return "success";
		} else {
			addErrorInfo("\u5BC6\u7801\u4FEE\u6539\u5931\u8D25\uFF01");
			return "input";
		}
	}

	public String getNewpassword() {
		return newpassword;
	}

	public void setNewpassword(String newpassword) {
		this.newpassword = newpassword;
	}

	public Userinfo getUser() {
		return user;
	}

	public void setUser(Userinfo user) {
		this.user = user;
	}

	public String getConfirmpassword() {
		return confirmpassword;
	}

	public void setConfirmpassword(String confirmpassword) {
		this.confirmpassword = confirmpassword;
	}

	public String getRoleInfo() {
		return roleInfo;
	}

	public void setRoleInfo(String roleInfo) {
		this.roleInfo = roleInfo;
	}

	private static final long serialVersionUID = 0x19511L;
	private Userinfo user;
	private String newpassword;
	private String confirmpassword;
	private String roleInfo;
}
